package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.api.ApiConstants;
import cn.glinks.liuhe.api.ApiRequest;
import cn.glinks.liuhe.api.jsonbean.UserInfo;
import cn.glinks.liuhe.app.AppApplication;
import cn.glinks.liuhe.app.AppConstant;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.baserx.RxSubscriber;
import cn.glinks.liuhe.utils.ActTransAnimUtils;
import cn.glinks.liuhe.utils.GsonUtils;
import cn.glinks.liuhe.utils.ToastUtils;


/**
 * Des:园区建议页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-21 14:43
 */
public class ParkProposalActivity extends BaseActivity {

    @BindView(R.id.tv_proposal)
    TextView mProposalText;

    // 变量
    private UserInfo mUserInfo;
    private String mParkName = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_park_proposal;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 加载用户信息
        mUserInfo = ((AppApplication) getApplicationContext()).getLoginUser();
        // 获取传递过来的数据
        Bundle bundle = this.getIntent().getExtras();
        mParkName = bundle.getString(AppConstant.BUNDLE_PARK_NAME, "");
        // 设置为透明状态栏
        SetTranslanteBar();
        // 发送网络请求, 请求获取园区建议数据
        mRxManager.add(ApiRequest.requestGetParkAdvice(mUserInfo.getJwt(), mParkName)
                .subscribe(new RxSubscriber<String>(mContext) {
                    @Override
                    protected void _onNext(String msg) {
                        if (GsonUtils.getIntValue(msg, "error") == 0) {
                            mProposalText.setText(GsonUtils.getValue(msg, "result"));
                        } else {
                            mProposalText.setText("没有建议内容");
                            String reason = GsonUtils.getValue(msg, "reason");
                            ApiConstants.showError(reason);
                        }
                    }

                    @Override
                    protected void _onError(String message) {
                        // TODO:处理失败响应
                        ToastUtils.showShort("获取园区建议失败:" + message);
                        mProposalText.setText("没有建议内容");
                    }
                }));
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 返回
    @OnClick(R.id.iv_back)
    public void clickBack() {
        ActTransAnimUtils.finishActionSlideLeftToRight(ParkProposalActivity.this);
    }

    /*--------------------------------------------------------------------------------
     *                          系统键处理
     ---------------------------------------------------------------------------------*/
    /**
     * 监听主页键,返回键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 返回
            ActTransAnimUtils.finishActionSlideLeftToRight(ParkProposalActivity.this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
